function [RET] = canny_trace_edge(EDGE, IMG, PHASE, T)

filter_90 = [0 0 0; 1 1 1; 0 0 0];
filter_135 = [0 0 1; 0 1 0; 1 0 0 ];
filter_0 = [0 1 0; 0 1 0; 0 1 0];
filter_45 = [1 0 0; 0 1 0; 0 0 1 ];

phase_0 = PHASE == 0;
phase_45 = PHASE == 45;
phase_90 = PHASE == 90;
phase_135 = PHASE == 135;

[height,width] = size(EDGE);

RET = EDGE;
MASK = IMG >= T;

OLD = MASK;
iteration_num = 0;
while isequal(OLD,RET) ~= 1
	iteration_num = iteration_num+1;
	OLD = RET;
	ret_0 = conv2(single(RET&phase_0),filter_0,'same') > 0;
	ret_45 = conv2(single(RET&phase_45),filter_45,'same') > 0;
	ret_90 = conv2(single(RET&phase_90),filter_90,'same') > 0;
	ret_135 = conv2(single(RET&phase_135),filter_135,'same') > 0;
	RET = (ret_0 | ret_45 | ret_90 | ret_135) & MASK;
end
iteration_num=iteration_num
